//
//  DataBaseCtrol.m
//  tcpoctest
//
//  Created by gump on 10/30/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "DataBaseCtrol.h"
#import "FirendInfo.h"
#import "requestCheckInfo.h"
#import "XMMessageInfo.h"
#import "UserInfo.h"
#import "requestCheckInfo.h"
#import "ChatContentInfo.h"

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMDatabasePool.h"
#import "FMDatabaseQueue.h"

#import <pthread.h>

#import "userdb.h"
#import "headimagedb.h"
#import "personInfodb.h"
#import "caredb.h"
#import "becaredb.h"
#import "messagedb.h"
#import "chatuserdb.h"
#import "bigimagedb.h"
#import "firendCircledb.h"
#import "FirendCirLocalPathdb.h"
#import "FirCirPictureIDdb.h"
#import "fircirnewsdb.h"
#import "FirCirNewIDCommentIDdb.h"
#import "FirCirCommentInfo.h"
#import "FirCirCommentdb.h"
#import "FirCirMailNewsdb.h"
#import "FirCirBgImagedb.h"

#import "giftListDataBase.h"
#import "giftInfoDatabase.h"
#import "giftInfoImageDataBase.h"
#import "giftInfoMusicDataBase.h"
#import "giftMessageDataBase.h"

#import "telepathydb.h"

@implementation DataBaseCtrol

static DataBaseCtrol *sharedInstance = nil;
static NSString      *databaseFilePath = @"";

static FMDatabase  *db = nil;

pthread_mutex_t mutexdb;



+(DataBaseCtrol*)shareControl
{
    if(!sharedInstance)
    {
        sharedInstance = [[super alloc] init];
    }
    return sharedInstance;
}

-(void)initMutex
{
    pthread_mutex_init(&mutexdb, 0);
}

-(void)openDB
{
#if !(TARGET_IPHONE_SIMULATOR)
    NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory
                                                                , NSUserDomainMask
                                                                , YES);
    databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"db.sql"];
#else
    databaseFilePath= [NSString stringWithFormat:@"%@%@",CUSTOMPATH,@"db.sql"];
#endif
    
    db = [FMDatabase databaseWithPath:databaseFilePath];
    
    
    
    self.userdatabase = [[userdb alloc] initWithDB:db withMutex:mutexdb];
    self.headimagedatabase = [[headimagedb alloc] initWithDB:db withMutex:mutexdb];
    self.personInfoDataBase = [[personInfodb alloc] initWithDB:db withMutex:mutexdb];
    self.messagedatabase = [[messagedb alloc] initWithDB:db withMutex:mutexdb];
    self.chatuserdatabase = [[chatuserdb alloc] initWithDB:db withMutex:mutexdb];
    self.bigimagedatabase = [[bigimagedb alloc] initWithDB:db withMutex:mutexdb];
    
    self.fircirnewdatabase = [[fircirnewsdb alloc] initWithDB:db withMutex:mutexdb];
    self.fircirPIDdatabase = [[FirCirPictureIDdb alloc] initWithDB:db withMutex:mutexdb];
    self.fircirdatabase = [[firendCircledb alloc] initWithDB:db withMutex:mutexdb];
    self.fircirlocaldatabase = [[FirendCirLocalPathdb alloc] initWithDB:db withMutex:mutexdb];
    self.fircirnewcommentdatabase = [[FirCirNewIDCommentIDdb alloc] initWithDB:db withMutex:mutexdb];
    self.fircircommentdatabase = [[FirCirCommentdb alloc] initWithDB:db withMutex:mutexdb];
    self.fircirmailnewsdatabase = [[FirCirMailNewsdb alloc] initWithDB:db withMutex:mutexdb];
    self.fircirbgimagedatabase = [[FirCirBgImagedb alloc] initWithDB:db withMutex:mutexdb];
    
    self.giftListImageDB  = [[giftListDataBase alloc] initWithDB:db withMutex:mutexdb];
    self.giftInfoDB = [[giftInfoDatabase alloc] initWithDB:db withMutex:mutexdb];
    self.giftinfoImageDB = [[giftInfoImageDataBase alloc] initWithDB:db withMutex:mutexdb];
    self.giftinfoMusicDB = [[giftInfoMusicDataBase alloc] initWithDB:db withMutex:mutexdb];
    self.giftMessageDB = [[giftMessageDataBase alloc] initWithDB:db withMutex:mutexdb];
    
    self.TelepDB = [[telepathydb alloc] initWithDB:db withMutex:mutexdb];
    
    [self creatTable];
    [self updateTable];
    
}

-(void)creatTable
{
    [self.userdatabase creatTable];
    [self.headimagedatabase creatTable];
    [self.personInfoDataBase creatTable];
    [self.messagedatabase creatTable];
    [self.chatuserdatabase creatTable];
    [self.bigimagedatabase creatTable];
    
    [self.fircirPIDdatabase creatTable];
    [self.fircirnewdatabase creatTable];
    [self.fircirdatabase creatTable];
    [self.fircirlocaldatabase creatTable];
    [self.fircirnewcommentdatabase creatTable];
    [self.fircircommentdatabase creatTable];
    [self.fircirmailnewsdatabase creatTable];
    [self.fircirbgimagedatabase creatTable];
    
    [self.giftListImageDB creatTable];
    [self.giftInfoDB creatTable];
    [self.giftinfoImageDB creatTable];
    [self.giftinfoMusicDB creatTable];
    [self.giftMessageDB creatTable];
    
    [self.TelepDB creatTable];
}

-(void)updateTable
{
    [self.messagedatabase updateTable];
}

-(void)initData
{
    [self.userdatabase initData];
    [self.headimagedatabase initData];
    [self.personInfoDataBase initData];
    [self.messagedatabase initData];
    [self.chatuserdatabase initData];
    [self.bigimagedatabase initData];
    
    [self.fircirnewdatabase initData];
    [self.fircirPIDdatabase initData];
    [self.fircirnewcommentdatabase initData];
    [self.fircirdatabase initData];
    [self.fircirlocaldatabase initData];
    [self.fircircommentdatabase initData];
    [self.fircirmailnewsdatabase initData];
    [self.fircirbgimagedatabase initData];
    
    [self.giftListImageDB initData];
    [self.giftInfoDB initData];
    [self.giftinfoImageDB initData];
    [self.giftinfoMusicDB initData];
    [self.giftMessageDB initData];
    
    [self.TelepDB initData];
}

-(void)AddComment:(FirCirCommentInfo *)fccinfo
{
    [self.fircirnewcommentdatabase insertNewComment:fccinfo.newsID
                                      withCommentID:fccinfo.commentID];
    [self.fircircommentdatabase insertComment:fccinfo];
    
    [self.fircirnewdatabase addCommentCountByID:fccinfo.newsID];
}

@end
